package com.tc.activiti.util.id;

import com.tc.activiti.util.AppUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import java.io.Serializable;

/**
 * 默认 ID 生成器。
 */
@Component
public class DefaultIdGenerator implements IdGenerator, InitializingBean,Serializable {

	/**
	 * serialVersionUID:
	 */
	private static final long serialVersionUID = 3405824291434808429L;
	private IdWorker idWorker;

	@Override
	public String getId() {
		return getUId().toString();
	}

	@Override
	public Long getUId() {
		if (idWorker == null)
			init();
		return idWorker.nextId();
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		init();
	}

	private void init() {
		idWorker = new IdWorker(Long.parseLong(AppUtil.getProperty("id.workerId", "0")),
				Long.parseLong(AppUtil.getProperty("id.datacenterId", "0")));
	}
}
